package com.example.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class HalfWebViewActivity extends Activity {
    private WebView mWebView;
    private JsInteration mJsEngine;
    private Animation mToTranslateIn;

    @Override
    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_webview_half);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        mJsEngine = new JsInteration(this);
        mWebView = findViewById(R.id.activity_main_webview);
        mWebView.setWebViewClient(new MyWebViewClient());
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        mWebView.addJavascriptInterface(mJsEngine, "android");

//        mWebView.loadUrl("https://trial.privacycn.com/#/separateAgree-corssBorder?templateId=4&id=10%3Fname%3D13012345678&type=phone");
        mWebView.loadUrl("https://trial.privacycn.com/#/separateAgree-corssBorder?templateId=4&id=10&name=13012345678&type=phone");

//        mWebView.loadUrl("https://www.baidu.com");

//        View bgView = findViewById(R.id.bgview);
//        bgView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mJsEngine.closeActivity();
//            }
//        });
    }

    //设置延迟开始的时间还蛮关键的，能决定动画是否平滑
    @Override
    protected void onStart() {
        super.onStart();
        if (mToTranslateIn == null) {
            mToTranslateIn = AnimationUtils.loadAnimation(this, R.anim.view_to_translate);
            mToTranslateIn.setFillAfter(true);
            mToTranslateIn.setStartOffset(200);
        }
        findViewById(R.id.view).startAnimation(mToTranslateIn);
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}
